#Boa:Dialog:Dialog1

import wx
import wx.grid


CONFIG_BATTLE_FIELD_HOR_SIZE = 10
CONFIG_BATTLE_FIELD_VER_SIZE = 10

def create(parent, app ):
    return Dialog1(parent, app )

[wxID_DIALOG1, wxID_DIALOG1BUTTONCLOSE, wxID_DIALOG1BUTTONUPDATE, 
 wxID_DIALOG1GRID1, wxID_DIALOG1STATICBOX1, 
] = [wx.NewId() for _init_ctrls in range(5)]

[wxID_DIALOG1TIMER1] = [wx.NewId() for _init_utils in range(1)]

class Dialog1(wx.Dialog):
    def _init_utils(self):
        # generated method, don't edit
        self.timer1 = wx.Timer(id=wxID_DIALOG1TIMER1, owner=self)
        self.Bind(wx.EVT_TIMER, self.OnTimer1Timer, id=wxID_DIALOG1TIMER1)


    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_DIALOG1, name='', parent=prnt,
              pos=wx.Point(382, 185), size=wx.Size(319, 305),
              style=wx.DEFAULT_DIALOG_STYLE, title=u'My battlefield')
        self._init_utils()
        self.SetClientSize(wx.Size(311, 278))
        self.Bind(wx.EVT_INIT_DIALOG, self.OnDialog1InitDialog)
        self.Bind(wx.EVT_ACTIVATE, self.OnDialog1Activate)

        self.buttonClose = wx.Button(id=wxID_DIALOG1BUTTONCLOSE, label=u'Close',
              name=u'buttonClose', parent=self, pos=wx.Point(96, 248),
              size=wx.Size(100, 25), style=0)
        self.buttonClose.SetToolTipString(u'Cancel')
        self.buttonClose.Bind(wx.EVT_BUTTON, self.OnButtonCloseButton,
              id=wxID_DIALOG1BUTTONCLOSE)

        self.grid1 = wx.grid.Grid(id=wxID_DIALOG1GRID1, name='grid1',
              parent=self, pos=wx.Point(16, 24), size=wx.Size(280, 176),
              style=0)
        self.grid1.SetColLabelSize(0)
        self.grid1.SetColMinimalAcceptableWidth(15)
        self.grid1.SetDefaultRowSize(15)
        self.grid1.SetDefaultColSize(15)
        self.grid1.SetRowLabelSize(0)
        self.grid1.SetCellHighlightROPenWidth(1)
        self.grid1.EnableEditing(False)
        self.grid1.Enable(True)
        self.grid1.SetSelectionForeground(wx.Colour(0, 0, 0))
        self.grid1.SetScrollLineX(15)
        self.grid1.SetScrollLineY(15)
        self.grid1.SetCellHighlightColour(wx.Colour(255, 255, 255))

        self.staticBox1 = wx.StaticBox(id=wxID_DIALOG1STATICBOX1,
              label=u'My battlefield', name='staticBox1', parent=self,
              pos=wx.Point(8, 2), size=wx.Size(296, 240), style=0)

        self.buttonUpdate = wx.Button(id=wxID_DIALOG1BUTTONUPDATE,
              label=u'Refresh', name=u'buttonUpdate', parent=self,
              pos=wx.Point(16, 208), size=wx.Size(75, 23), style=0)
        self.buttonUpdate.SetToolTipString(u'Reloads information from server')
        self.buttonUpdate.Bind(wx.EVT_BUTTON, self.OnButtonUpdateButton,
              id=wxID_DIALOG1BUTTONUPDATE)

    def __init__(self, parent, app ):
        self._init_ctrls(parent)
        self.grid1_created = False
        self.app = app 
        self.SetTitle( self.app.player_name + ': battlefield' )
                
    def OnButtonCloseButton(self, event):
        self.Show(False)

    def OnDialog1InitDialog(self, event):
        
        if( self.grid1_created == False ):
            self.grid1.CreateGrid( 10 , 10, 0 )

            self.grid1.DisableDragRowSize()
            self.grid1.DisableDragColSize()
            self.grid1_created = True
            self.timer1.Start(2000)

    def OnButtonUpdateButton(self, event):

        battlefield_str = self.app.server_proxy.get_battlefield()
        
        for i in range( 0 , CONFIG_BATTLE_FIELD_VER_SIZE ):
            for j in range( 0 , CONFIG_BATTLE_FIELD_HOR_SIZE ):
                
                sq = battlefield_str[ j + i * CONFIG_BATTLE_FIELD_HOR_SIZE ]
                if( sq == 's' ):
                    color = wx.BLACK
                elif( sq == '*' ):
                    color = wx.RED
                elif( sq == '.' ):
                    color = wx.LIGHT_GREY
                else:
                    color = wx.WHITE
                    
                color = self.grid1.SetCellBackgroundColour( i , j , color)

        self.grid1.ForceRefresh()

    def OnDialog1Activate(self, event):
        self.OnButtonUpdateButton( None )

    def OnTimer1Timer(self, event):
        self.OnButtonUpdateButton( None )
        
